// Google Map
var map;

// Trajet
var trajet = [];
trajet.push(new google.maps.LatLng("50.61262", "3.14201"));
trajet.push(new google.maps.LatLng("50.61264", "3.14079"));
trajet.push(new google.maps.LatLng("50.6124", "3.14083"));
trajet.push(new google.maps.LatLng("50.61212", "3.14067"));
trajet.push(new google.maps.LatLng("50.61205", "3.14098"));
trajet.push(new google.maps.LatLng("50.61199", "3.14107"));
trajet.push(new google.maps.LatLng("50.61195", "3.14108"));
trajet.push(new google.maps.LatLng("50.61187", "3.14138"));
trajet.push(new google.maps.LatLng("50.61153", "3.14166"));
trajet.push(new google.maps.LatLng("50.61153", "3.14166"));
trajet.push(new google.maps.LatLng("50.61129", "3.14146"));
trajet.push(new google.maps.LatLng("50.61124", "3.14129"));
trajet.push(new google.maps.LatLng("50.61101", "3.14131"));

// test des commentaires
var comment = [];

// commentaire déja défini
var test = [];
test.push(new google.maps.LatLng("50.61263950595267", "3.140804985423074"));
test.push(new google.maps.LatLng("50.61263950595267", "3.140804985423074"));
test.push(new google.maps.LatLng("50.61262895656886", "3.1420098770313416"));
var testPolyline;


// directions options
var optionsDirections = {
	// draggable : true
	polylineOptions : {
		strokeColor : '#f42418'
	}
};

var maPolyline;

var directionsDisplay = new google.maps.DirectionsRenderer();
var directionsService = new google.maps.DirectionsService();

// par défaut "Villeneuve d'ascq, campus"
var defaultLat = 50.6093453493974;
var defaultLng = 3.1418323516845703;


function initialize() {
	// options de google maps
	var mapOptions = {
		disableDoubleClickZoom : true,
		// centre de la map
		center : {
			lat : defaultLat,
			lng : defaultLng
		},
		// suppression des panneaux de control de google map
		panControl : false,
		streetViewControl : false,
		mapTypeControl : false,
		// réglage du zoom
		zoom : 16,
		maxZoom : 18,
		preserveViewport : true

	};

	map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

	// gestion d'un double click
	google.maps.event.addListener(map, 'dblclick', function(event) {});
	
	drawPolyline(trajet);
}

// fonction de dessin
// location.k = lat
// location.B = lng
function drawComment(location) {
	// on ajoute les coordonnées dans une array
	directionsDisplay = new google.maps.DirectionsRenderer(optionsDirections);
	comment.push(new google.maps.LatLng(location.k, location.B));
	// On dessine petit à petit notre trajet
	if (comment.length > 1) {
		var commenPolyline = new google.maps.Polyline({
			map : map,
			path : comment,
			clickable : true,
			strokeColor : "#FF0000",
			zIndex : 2
		});
	}
}

// commentaire onhover
function drawComment2(){
	testPolyline = new google.maps.Polyline({
		map : map,
		path : test,
		clickable : true,
		strokeColor : "#FF0000",
		zIndex : 2
	});
}

function removeComment(){
	testPolyline.setMap(null);
}

function drawPolyline(trajet) {
	// polyline options
	maPolyline = new google.maps.Polyline({
		map : map,
		path : trajet,
		clickable : true,
		strokeColor : "#58ACFA",
		zIndex : 1
	});
	
	google.maps.event.addListener(maPolyline, 'click', function(event) {     
		drawComment(event.latLng);
		console.log(comment);
	});
}

google.maps.event.addDomListener(window, 'load', initialize);

